// 获取当前用户名
var name1=getCookie('name')
if(!name1){
    alert("还没有登录，请登录")
    location='./denglu.html?url='+location.href
}
var box=document.querySelector('.w')
var carts=localStorage.getItem(name1) || "[]";
carts=eval('('+carts+')')
show1()
function show1(){
    if(carts.length>0){
        var bool = carts.every(item => {
            return item.is_select == 1
        })
        var str1=`
        <h2 class="biaoti">购物袋<span>(<span>${gwd()[0]}</span>)</span></h2>
        `
        carts.forEach(item=>{
            str1+=`
            <div class="leftgwc">
            <div class="fqq1">
            <div class="fq">
            <div class="xuankuan">
                <input type="checkbox" name="xuan" ${item.is_select == 1 ? "checked" : ''}  data-id='${item.goods_id}'>
            </div>
            <div class="gwczp">
                <img src="${item.goods_spzp}" alt="">
            </div>
            <div class="gwcxx">
                <span class="spname">${item.goods_name}</span>
                <span class="sqj">市场价：¥<span>${item.goods_scprice}</span></span>
                <span class="jg_jg">商品价格：¥<span>${item.goods_price}</span></span>
                <span class="dell" data-id='${item.goods_id}'>移除</span>
            </div>
            <div class="tj">
                <div class="btn-group" role="group" aria-label="...">
                    <button type="button" class="btn btn-default" data-id='${item.goods_id}' ${item.cart_number == 1 ? "disabled" : ''}>-</button>
                    <button type="button" class="btn btn-default">${item.cart_number}</button>
                    <button type="button" class="btn btn-default" data-id='${item.goods_id}'' ${item.goods_num == item.cart_number ? "disabled" : ""}>+</button>
                </div>
                <div class="btn-group" role="group" aria-label="..." id="ms">
                    <button type="button" class="btn btn-default">S</button>
                    <button type="button" class="btn btn-default">M</button>
                    <button type="button" class="btn btn-default">L</button>
                                </div>
                            </div>
                        </div>
                        </div>
                    </div>
            `
        })
        str1+=`
        <div class="rightgwc">
        <h4>价格详情</h4>
        <div class="spje">
            <p>
                <span>市场价</span>
                <span><em>￥</em>${gwd()[1]}</span>
            </p>
        </div>
        <div class="spje">
            <p>
                <span>商品金额</span>
                <span><em>￥</em>${gwd()[1]}</span>
            </p>
        </div>
        <div class="spje">
            <p>
                <span>合计</span>
                <span><em>￥</em>${total1()[1]}</span>
            </p>
        </div>
        <div class="quanxuan">
            <input type="checkbox" name="quan" ${bool ? "checked" : ''}>
            <div class="quan" >全选</div>
        </div>
        <div class="jisuan">结算(<span>${total1()[0]}</span>)
        </div>
        <div class="gwczp2"><img src="../img/gwczp.png" alt=""></div>
        </div>
        `
        box.innerHTML=str1

    }else{
     var  str=`
       <div class="gwdk">
       <div class="w">
           <h2>购物袋</h2>
           <p>您的购物袋是空的，请你选购商品。</p><br>
           <button><a href="./spzs.html">选购商品</a></button>
       </div>
   </div>
       `
       box.innerHTML=str
    }
}
box.onclick=function(e){
    var e=e||window.event
    var target =e.target || e.srcElement
    if (target.name == "quan") {
        carts.forEach(item => {
            if (target.checked) {
                item.is_select = 1
            } else {
                item.is_select = 0
            }
        })
        localStorage.setItem(name1, JSON.stringify(carts))
        show1()
    }
    if(target.name=="xuan"){
        var id=target.getAttribute("data-id")
        carts.forEach(item=>{
            if(item.goods_id==id){
                item.is_select=(item.is_select==1) ? 0 : 1
            }
        })
        localStorage.setItem(name1,JSON.stringify(carts))
        show1()
    }
    if(target.innerHTML=="+"){
        var id=target.getAttribute('data-id')
        console.log()
        carts.forEach(item => {
            if (item.goods_id == id) {
                item.cart_number++
            }
        })
        localStorage.setItem(name1, JSON.stringify(carts))
        show1()
    }
    if (target.innerHTML == "-") {
        var id = target.getAttribute('data-id')
        carts.forEach(item => {
            if (item.goods_id == id) {
                item.cart_number--
            }
        })
        localStorage.setItem(name1, JSON.stringify(carts))
        show1()
    }
    if (target.innerHTML == "移除") {
        var id = target.getAttribute("data-id")
        carts = carts.filter(item => {
            return item.goods_id != id
        })
        localStorage.setItem(name1, JSON.stringify(carts))
        show1()
    }
}

var by=document.querySelector('.jisuan')
by.onclick=function js(){
    if(confirm(`你确定要花￥${total1()[1]}购买吗`)){
        //过滤当前选中的商品
        carts=carts.filter(item=>{
            return item.is_select!=1
        })
        //重新把当前数组存储到localStorage中
        localStorage.setItem(name1, JSON.stringify(carts))
        show1()
    }
}
function total1(){
    var num=0 //所选商品数量
    var sum=0 //所选商品价格
    //遍历所有商品
    carts.forEach(item=>{
        //判断当前商品是否被选中
        if(item.is_select==1){
            //累加需要购买的商品数量
            num+=parseInt(item.cart_number)
            //累加所有需要购买的商品小计
            sum+=parseInt(item.cart_number)*parseFloat(item.goods_price)
        }
    })
    return [num,sum]
}
function gwd(){
    var num=0
    var sum=0
    carts.forEach(item=>{
        num+=parseInt(item.cart_number)
        if(item .is_select==1){
            sum+=parseInt(item.goods_price)
        }
    })
    return [num,sum]
}